package com.example.demo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/response")
public class ResponseController {
    @ResponseBody
    @RequestMapping("/returnPage")
    public String returnPage(){
        return "返回数据index.html";
    }

    /**
     *
     * @return返回页面
     */
    @RequestMapping("/returnPage2")
    public String returnPage2(){
        return "/index.html";
    }


    @RequestMapping("/returnHtml")
    @ResponseBody
    public String returnHtml(){
        return "<h1>这是返回了html</h1>";
    }

    @RequestMapping("/returnJson")
    @ResponseBody
    public Person returnJson(){
        Person person=new Person("zyh",12);
        return person;
    }

    @RequestMapping("/returnMap")
    @ResponseBody
    public Map<String, Person> returnMap(){
        Map<String,Person> map=new HashMap<>();
        map.put("one",new Person("aaa",1));
        map.put("two",new Person("bbb",2));
        map.put("three",new Person("ccc",3));
        return map;
    }

    @RequestMapping("/returnJson2")
    @ResponseBody
    public String returnJson2(){
        return "{\n" +
                "    \"one\": {\n" +
                "        \"name\": \"aaa\",\n" +
                "        \"age\": 1\n" +
                "    },\n" +
                "    \"two\": {\n" +
                "        \"name\": \"bbb\",\n" +
                "        \"age\": 2\n" +
                "    },\n" +
                "    \"three\": {\n" +
                "        \"name\": \"ccc\",\n" +
                "        \"age\": 3\n" +
                "    }\n" +
                "}";
    }

    @RequestMapping(value = "/returnJson3",produces ="application/json")
    @ResponseBody
    public String returnJson3(){
        return "{\n" +
                "    \"one\": {\n" +
                "        \"name\": \"aaa\",\n" +
                "        \"age\": 1\n" +
                "    },\n" +
                "    \"two\": {\n" +
                "        \"name\": \"bbb\",\n" +
                "        \"age\": 2\n" +
                "    },\n" +
                "    \"three\": {\n" +
                "        \"name\": \"ccc\",\n" +
                "        \"age\": 3\n" +
                "    }\n" +
                "}";
    }

    @RequestMapping("/setStatus")
    @ResponseBody
    public String setStatus(HttpServletResponse response){
        response.setStatus(404);
        return "设置状态码成功";
    }

    @RequestMapping("/setHeader")
    @ResponseBody
    public String setHeader(HttpServletResponse response){
        response.setHeader("myhead","zyh");
        return "设置header成功";
    }

    @RequestMapping("/setContentType")
    @ResponseBody
    public String setContentType(HttpServletResponse response){
        response.setHeader("Content-Type","application/json");
        return "{\n" +
                "    \"one\": {\n" +
                "        \"name\": \"aaa\",\n" +
                "        \"age\": 1\n" +
                "    },\n" +
                "    \"two\": {\n" +
                "        \"name\": \"bbb\",\n" +
                "        \"age\": 2\n" +
                "    },\n" +
                "    \"three\": {\n" +
                "        \"name\": \"ccc\",\n" +
                "        \"age\": 3\n" +
                "    }\n" +
                "}";
    }

}
